package com.atguigu.guli.service.edu.controller.admin;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.vo.AdminCourseItemVo;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/admin/edu/course")

public class AdminCourseController {

    @Autowired
    private CourseService courseService;

    //根据id删除课程
    @DeleteMapping("/deleteById/{id}")
    public R delteById(@PathVariable("id") String id){
        courseService.delteById(id);
        return R.ok();
    }

    @DeleteMapping("/removeById/{id}")
    public R removeById(@PathVariable("id") String id){
        courseService.delteById(id);
        return R.ok();
    }

    //保存课程基本信息
    //@CachePut(value = "ads",key = "'cache'")
    @PostMapping("/saveCourseInfo")
    public R saveCourseInfo(@RequestBody  CourseInfoForm courseInfoForm){
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("id",courseId);
    }

    //回显已经保存的课程信息
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable("courseId") String courseId){
       CourseInfoForm courseInfoForm= courseService.getCourseInfo(courseId);
       return R.ok().data("item",courseInfoForm);
    }

    //分页查询课程
    @GetMapping("/queryPage/{pageNum}/{pageSize}")
    public R queryPage(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){
       Page<AdminCourseItemVo> page= courseService.queryCourseItemVoPage(pageNum,pageSize);
       return R.ok().data("page",page);
    }
    //更新课程信息
    @PutMapping("/updateById/{courseId}")
    public R updateById(@RequestBody CourseInfoForm courseInfoForm,@PathVariable("courseId") String courseId){
        courseService.updateCourseInfoFormById(courseInfoForm,courseId);
        return R.ok();

    }

    //查询课程发布信息
    @GetMapping("/getCoursePublishVo/{id}")
    public R getCoursePublishVo(@PathVariable("id") String id){
       AdminCourseItemVo adminCourseItemVo= courseService.getCoursePublishVo(id);
       return R.ok().data("item",adminCourseItemVo);
    }

    //更新课程的发布状态
    //@CacheEvict(value = "ads",key = "'cache'")
    @PutMapping("/publish/{id}")
    public R publish(@PathVariable("id") String id){
        courseService.update(new LambdaUpdateWrapper<Course>()
                .set(Course::getStatus , "Normal")
                .set(Course::getPublishTime , new Date())
                .eq(Course::getId , id));
        return R.ok();
    }

    //取消发布课程状态
    @PutMapping("/stopPublish/{id}")
    public R stopPublish(@PathVariable("id") String id){
        courseService.update(new LambdaUpdateWrapper<Course>()
                .set(Course::getStatus , "Draft")
                .set(Course::getPublishTime , new Date())
                .eq(Course::getId , id));
        return R.ok();
    }
    //7、查询指定日期发布课程的数量
    @GetMapping("/getCoursePublishNum/{day}")
    public R getCoursePublishNum(@PathVariable("day")String day){
        long count = courseService.count(new QueryWrapper<Course>()
                .eq("date(publish_time)", day));
        return R.ok().data("num",count);
    }

}
